<?php

namespace backend\helpers;

use yii\helpers\Html;

class ActionButton
{

    public static function create($title, $permission)
    {
        if(\Yii::$app->user->can($permission))
        {
            echo Html::a($title, ['create'], ['class' => 'btn btn-success']);
        }
    }

    public static function createWithURI($title, $uriArr, $permission)
    {
        if(\Yii::$app->user->can($permission))
        {
            echo Html::a($title, $uriArr, ['class' => 'btn btn-success']);
        }
    }

    public static function update($item_id, $permission)
    {
        if(\Yii::$app->user->can($permission))
        {
            echo Html::a('Редактировать', ['update', 'id' => $item_id], ['class' => 'btn btn-primary']);
        }
    }

    public static function updateWithURI($title, $uriArr, $permission)
    {
        if(\Yii::$app->user->can($permission))
        {
            echo Html::a($title, $uriArr, ['class' => 'btn btn-primary']);
        }
    }

    public static function delete($item_id, $warningMessage, $permission)
    {
        if(\Yii::$app->user->can($permission))
        {
            echo Html::a('Удалить', ['delete', 'id' => $item_id], [
                'class' => 'btn btn-danger',
                'data' => [
                    'confirm' => $warningMessage,
                    'method' => 'post',
                ],
            ]);
        }
    }

    public static function deleteWithURI($title, $uriArr, $warningMessage, $permission)
    {
        if(\Yii::$app->user->can($permission))
        {
            echo Html::a($title, $uriArr, [
                'class' => 'btn btn-danger',
                'data' => [
                    'confirm' => $warningMessage,
                    'method' => 'post',
                ],
            ]);
        }
    }

    public static function deleteImage($item_id, $image, $warningMessage, $permission)
    {
        if(\Yii::$app->user->can($permission))
        {
            echo Html::a('Удалить', ['delete-image', 'id' => $item_id, 'image_name' => $image], [
                'class' => 'btn btn-danger',
                'data' => [
                    'confirm' => $warningMessage,
                    'method' => 'post',
                ],
            ]);
        }
    }

    public static function SimpleLink($title, $uri, $permission, $btnClass = 'btn-success')
    {
        if(\Yii::$app->user->can($permission))
        {
            echo Html::a($title, [$uri], ['class' => 'btn ' . $btnClass]);
        }
    }

    public static function dictionaryLink($title, $uri, $permission)
    {
        if(\Yii::$app->user->can($permission))
        {
            $content = '<span class="glyphicon glyphicon-th-list"></span> ' . $title;
            echo Html::a($content, [$uri], ['class' => 'btn btn btn-primary']);
        }
    }

    public static function custom($title, $permission, $action, $actionParameter = null, $btnClass = 'btn-primary', $selfBlocked = false, $target = '_self')
    {
        if(\Yii::$app->user->can($permission))
        {
            $actionArr = [$action];
            if($actionParameter)
            {
                $actionArr = array_merge($actionArr, $actionParameter);
            }
            if($selfBlocked == false)
            {
                echo Html::a($title, $actionArr, ['class' => 'btn ' . $btnClass, 'target' => $target]);
            }
            else 
            {
                echo Html::a($title, $actionArr, [
                    'class' => 'btn ' . $btnClass,
                    //'onclick' => '(function ( $event ) { this.classList.add("disabled"); this.innerHTML="Выполняем..."; return false; })();'
                    'onclick' => 'alert("Аудио создается. Нажмите ОК и дождитесь перезагрузки страницы.")',
                ]);
            }
            
        }
    }

}